﻿using Coolzon.UI.Core.Animation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using RT = Coolzon.Metadata.Runtime;

namespace Coolzon.UI.Core
{ 
    public partial class Frame
    {
        /// <summary>
        /// 缩放
        /// </summary>
        /// <param name="factor">0.1-2.0</param>
        /// <param name="pointInFrame"></param>
        public void Zoom(double factor, Point pointInFrame)
        {
            if (factor < 0.05d)
            {
                factor = 0.05d;
            }
            else if (factor > 2.0d)
            {
                factor = 2.0d;
            }

            //if (factor != this.ZoomFactor)
            //{
            //    Point pointInScene = this.TranslatePoint(pointInFrame, _scrollViewer);

            //    //double centerX = 1.0d;

            //    _scaleTransform.ScaleX = factor;
            //    _scaleTransform.ScaleY = factor;
            //    //Debug.WriteLine(string.Format("zoom:{0}", factor));
            //    double oldFactor = this.ZoomFactor;
            //    this.ZoomFactor = factor;

            //    if (OnZooming != null)
            //    {
            //        OnZooming(oldFactor, this.ZoomFactor);
            //    }
            //}
        }

        /// <summary>
        /// 显示组件,
        /// 但是需要调用Locator来激活场景或场景的某部分
        /// </summary>
        /// <param name="part"></param>
        /// <param name="switchAnimation"></param>
        public void ShowScene(RT.Solution solutionMetadata, Scene scene, SwitchAnimation switchAnimation)
        {
            this.PartContext = CreatePartContext(solutionMetadata, scene.SceneMatadata);

            scene.Init(this.PartContext);

            this._enteringPart = scene;
            this._leaveingPart = this._currentPart;
            //// 关闭放大镜和激光笔
            //this.HideMagnify();
            //this.HideLaserPointer();

            if (this._leaveingPart != null)
            {
                this._leaveingPart.Destroy();
                this._container.Children.Remove(this._leaveingPart);
                this._leaveingPart = null;
            }

            if (this._enteringPart == null)
            {
                this._currentPart = null;
                return;
            }

            this._enteringPart.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            this._enteringPart.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            this._enteringPart.Loaded += _enteringPart_Loaded;
            this._container.Children.Add(this._enteringPart);
        }

        void _enteringPart_Loaded(object sender, RoutedEventArgs e)
        {
            if (_currentPart != null)
            {
                _currentPart.Destroy();
            }
            _currentPart = (sender as Scene);


            if (OnSceneOpened != null)
            {
                OnSceneOpened(_currentPart);
            }
            //// maybe need async
            //Action<Scene> action = new Action<Scene>(AsyncActive);
            //action.BeginInvoke(_currentPart, (p) =>
            //{

            //}, null);
        }

        private void AsyncActive(Scene scene)
        {
            scene.Dispatcher.BeginInvoke(new Action(() =>
            {
                //scene.Active(this.PartContext);
                if (OnSceneOpened != null)
                {
                    OnSceneOpened(_currentPart);
                }
            }));
        }
    }
}
